/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.momarketing.entity.MicroPage;
import com.boxin.ims.modules.momarketing.entity.Project;
import com.boxin.ims.modules.momarketing.service.MicroPageService;
import com.boxin.ims.modules.momarketing.service.ProjectService;

/**
 * 微网页Controller
 * @author Jakemanse
 * @version 2013-11-22
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/momarketing/microPage")
public class MicroPageController extends BaseController {

	@Autowired
	private MicroPageService microPageService;
	
	@Autowired
	private ProjectService projectService;
	
	@ModelAttribute
	public MicroPage get(@RequestParam(required=false) Long id) {
		if (id != null){
			return microPageService.get(id);
		}else{
			return new MicroPage();
		}
	}
	
	@RequiresPermissions("momarketing:microPage:view")
	@RequestMapping(value = {"list", ""})
	public String list(MicroPage microPage, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			microPage.setUser(user);
		}
        Page<MicroPage> page = microPageService.find(new Page<MicroPage>(request, response), microPage); 
        model.addAttribute("page", page);
		return "modules/momarketing/microPageList";
	}

	@RequiresPermissions("momarketing:microPage:view")
	@RequestMapping(value = "form")
	public String form(MicroPage microPage, Model model) {
	
		
		User user = UserUtils.getUser();
		if (microPage.getUser() == null || microPage.getUser().getId() <1){
			microPage.setUser(user);
		}
		Project project = projectService.getByUserId(user.getId());
		if(project == null){
			model.addAttribute("message", "请先添加微网站后再添加微页面.");
			model.addAttribute("flag", "0");
			return "modules/momarketing/microPageForm";
		}
		microPage.setProject(project);
		
		model.addAttribute("microPage", microPage);
		return "modules/momarketing/microPageForm";
	}

	@RequiresPermissions("momarketing:microPage:edit")
	@RequestMapping(value = "save")
	public String save(MicroPage microPage, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, microPage)){
			return form(microPage, model);
		}
		microPageService.save(microPage);
		addMessage(redirectAttributes, "保存微网页'" + microPage.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/microPage/?repage";
	}
	
	@RequiresPermissions("momarketing:microPage:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		microPageService.delete(id);
		addMessage(redirectAttributes, "删除微网页成功");
		return "redirect:"+Global.ADMIN_PATH+"/momarketing/microPage/?repage";
	}

}
